﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;

using ProjectBase.Core;

using $prjsn$.Entity.Services.HowTos;
using $prjsn$.Common;
using $prjsn$.Common.Business.Services.HowTos;
using $prjsn$.Common.Data.Services.HowTos;
using $prjsn$.Common.Validation;
using $safeprojectname$;

namespace $safeprojectname$.Services.HowTos
{
    public class SimpleEntityFilterValidator : FilterValidatorBase<SimpleEntity, SimpleEntityFilter, string>
    {
        protected override void Init(RuleCollection<SimpleEntityFilter> rules)
        {
            base.Init(rules);
        }
    }

    public class SimpleEntityValidator : EntityValidatorBase<SimpleEntity, string, ISimpleEntityDao>
    {
        protected override void Init(RuleCollection<SimpleEntity> rules)
        {
            rules.For(entity => entity.RealValue).RequireValueBetween(1000, 2000);
            rules.For(entity => entity.Description).RequireLengthBetween(2, 20);

            // unique, user typed ID
            rules.For(entity => entity.NewID).RequireLengthBetween(3, 50)
                 .WhenCreating()
                 .BreakOnError();
            rules.IsUnique(entity => Dao.Find(new SimpleEntityFilter { IdExact = entity.ID }), "ID")
                 .WhenCreating()
                 .BreakOnError();

            // unique, NaturalKey
            rules.For(entity => entity.NaturalKey).RequireLengthBetween(5, 20)
                 .BreakOnError();
            rules.IsUnique(entity => Dao.Find(entity.ID, new SimpleEntityFilter { NaturalKeySearch = entity.NaturalKey }))
                 .BreakOnError();

            // delete
            rules.Assure(IsSimpleEntityInUse)
                 .WhenDeleting()
                 .AndCanDelete();
        }

        protected virtual ValidationResult IsSimpleEntityInUse(SimpleEntity entity, out ValidationMessage message)
        {
            message = null;
            var result = true;
            var innerMessages = new List<ValidationMessage>();

            // as Combo
            var filter = new ComplexEntityFilter
            {
                TakeRows = 1,
                SimpleCombo = entity,
            };
            var asCombo = ComplexEntityFacade.Find(filter).FirstOrDefault();

            if (asCombo.Is())
            {
                innerMessages.Add(new ValidationMessage(Str.Messages.ItemIsUsedForEntity4)
                {
                    entity,
                    asCombo,
                    Str.Controllers.ComplexEntity,
                    "SimpleCombo",
                });
                result = false;
            }

            // as Auto
            filter = new ComplexEntityFilter
            {
                TakeRows = 1,
                SimpleAuto = entity,
            };
            var asAuto = ComplexEntityFacade.Find(filter).FirstOrDefault();
            if (asAuto.Is())
            {
                innerMessages.Add(new ValidationMessage(Str.Messages.ItemIsUsedForEntity4)
                {
                    entity,
                    asAuto,
                    Str.Controllers.ComplexEntity,
                    "SimpleAuto",
                });
                result = false;
            }

            // as Async
            filter = new ComplexEntityFilter
            {
                TakeRows = 1,
                SimpleAsync = entity,
            };
            var asAsync = ComplexEntityFacade.Find(filter).FirstOrDefault();
            if (asAsync.Is())
            {
                innerMessages.Add(new ValidationMessage(Str.Messages.ItemIsUsedForEntity4)
                {
                    entity,
                    asAsync,
                    Str.Controllers.ComplexEntity,
                    "SimpleAsync",
                });
                result = false;
            }

            if (result)
            {
                return ValidationResult.IsValid;
            }
            message = new ValidationMessage(Str.Messages.CannotDelete1, Severity.Warning)
            {
                entity.ToDisplay(),
            };
            message.InnerMessages = innerMessages;
            return ValidationResult.Error;
        }

        [CLSCompliant(false)]
        public virtual IComplexEntityFacade ComplexEntityFacade { protected get; set; }
    }
}